import SelectUnit from './component/SelectUnit';
import { login_way, serverLogin } from './common/Function';
import JsBridge from './service/JsBridge';

$(function () {

    JsBridge.sendRequest("login.history").then(resp => {
        console.log(resp.data);
    });

    JsBridge.getConfig().then(config => {
    	console.log(config)
        if (config && config.LoginID && config.EncyptPassword && config.UnitUrl && config.UnitUrl2 && config.UnitName) {
        	console.log(config.EncyptPassword)
        	console.log("密码")
//          serverLogin(config.UnitUrl, config.LoginID, config.EncyptPassword)
//          .then(resp => {
              // 登陆未成功
//            if (!resp) {
              	 
              	console.log(config)
              	
              	var logs=JSON.parse(config.yxtLog);

              	var pass=logs.logs[0].pass;
              	console.log('登录历史消息')
              	JsBridge.sendRequest("user.password", {password: pass}).then(resp => {
              		serverLogin(config.UnitUrl, config.LoginID, resp.data.password,logs.logs[0].UnitUrl2)
              	}).catch((resp) => {
              		 $("#user").val(config.LoginID);
                  $("#pass").val(logs.logs[0].pass);         //添加密码
                $("#choose").val(config.UnitName || '');
                $('.container-fluid').css('opacity', 1);
                login_way(config.UnitUrl,config.UnitUrl2)
              	})
              	
//              $("#user").val(config.LoginID);
//                $("#pass").val(logs.logs[0].pass);         //添加密码
//              $("#choose").val(config.UnitName || '');
//              $('.container-fluid').css('opacity', 1);
//              login_way(config.UnitUrl)
//            }
//          })
        } else {
            $('.container-fluid').css('opacity', 1);
        }
    });

    const prompt = document.getElementsByClassName("prompt")[0];
    const lostpass = document.getElementsByClassName("lostpassword")[0];
    const company = document.getElementById('choose');

  //   const fistLog= JSON.parse(localStorage.getItem('yxtLog'));
  //   if(fistLog){
  //   	if(fistLog.logs.length>0){			//登录赋值
	// 		$("#user").val(fistLog.logs[0].user);
	// //  	$("#pass").val(fistLog.logs[0].pass);
	//     	$("#choose").val(fistLog.logs[0].UnitName);
	//     	console.log('登录赋值')
	// 	}
  //   }


    lostpass.onclick = function () {                       //显示忘记密码
        prompt.style.display = "block";
    }
    prompt.children[2].onclick = function () {              //取消忘记密码提示
        prompt.style.display = "none";
    }

    var lick = document.getElementsByClassName("lick")[0];
    lick.onclick = function () {                                 //单位选择
        // lick_choose.style.display = "block";
        // $("#select-unit").modal();

        SelectUnit.bindOk((data)=>{
        	 console.log(data)
        	 console.log('查看url2')
          if(data){
            const {Unit_ID, UnitUrl, UnitName,UnitUrl2} = data;

            company.value = UnitName;
            company.dataset.id = Unit_ID;
            
            login_way(UnitUrl,UnitUrl2); // 绑定登陆事件
          }
        })
        SelectUnit.show();
    }

});
